package com.faithbj.device.model.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.ForeignKey;

import com.faithbj.device.utils.DateUtils;


@Entity
public class Reservation extends BaseEntity {
	/**
	 * 按时间收费成本,单位是元/小时
	 * 
	 */
	public static final Integer TIME_COST=1;
	/**
	 * 按样品收费成本,单位是元/个
	 */
	public static final Integer COUNT_COST=2;
	/**
	 * 已经结算
	 * 
	 */
	public static final Integer CHARGED=1;
	/**
	 * 未结算
	 */
	public static final Integer UNCHARGED=2;

	private static final long serialVersionUID = 6093475454055896240L;
	
	private Date beginDate;// 开始日期

	private Date endDate;// 结束日期
	
	private DeviceInfo device;
	
	/**
	 *  费用类型
	 */
	private Integer costType;
	/**
	 *  样品个数
	 */	
	private Integer sampleCount;
	/**
	 * 预算
	 */
	private Integer budget;
	/**
	 * 决算
	 */
	private Integer revenue;
	/**
	 * 结算情况1结算2未结算
	 */
	private Integer chargeStatus;
	
	private Members member;
	
	@Column(name="begin_date",nullable=false)
	public Date getBeginDate() {
		return beginDate;
	}
	@Column(name="end_date",nullable=false)
	public Date getEndDate() {
		return endDate;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@ForeignKey(name="fk_reserve_device")
	public DeviceInfo getDevice() {
		return device;
	}
	@Column(name="cost_type")
	public Integer getCostType()
	{
		return costType;
	}
	@Column
	public Integer getBudget() {
		return budget;
	}
	@Column
	public Integer getRevenue() {
		return revenue;
	}
	@Column(name="sample_count")
	public Integer getSampleCount() {
		return sampleCount;
	}
	@Column(name="charge_status")
	public Integer getChargeStatus() {
		return chargeStatus;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@ForeignKey(name="fk_reserve_member")
	public Members getMember() {
		return member;
	}
	
	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	public void setMember(Members member) {
		this.member = member;
	}
	public void setDevice(DeviceInfo device) {
		this.device = device;
	}
	public void setCostType(Integer costType) {
		this.costType = costType;
	}
	public void setBudget(Integer budget) {
		this.budget = budget;
	}
	public void setRevenue(Integer revenue) {
		this.revenue = revenue;
	}
	public void setSampleCount(Integer sampleCount) {
		this.sampleCount = sampleCount;
	}
	public void setChargeStatus(Integer chargeStatus) {
		this.chargeStatus = chargeStatus;
	}
	
	@Transient
	public Integer getDifferenceValue(){
		Integer calced=DateUtils.betweenHours(this.getBeginDate(),this.getEndDate());
		return calced;
	}
	
	
	/**
	 * 处理搜索添加时是""的情况，把""换成null
	 * @return
	 */
	@Transient
	public void handleBlankProperties()
	{
		if(member!=null&&StringUtils.isBlank(member.getUsername()))
			member.setUsername(null);
		else if(member!=null&&StringUtils.isNotBlank(member.getUsername()))
			member.setUsername(member.getUsername().trim());
		
		if(member!=null&&StringUtils.isBlank(member.getRealName()))
			member.setRealName(null);
		else if(member!=null&&StringUtils.isNotBlank(member.getRealName()))
			member.setRealName(member.getRealName().trim());
		
		
		if(device!=null&&StringUtils.isBlank(device.getPropertyName()))
			device.setPropertyName(null);
		else if(device!=null&&StringUtils.isNotBlank(device.getPropertyName()))
			device.setPropertyName(device.getPropertyName().trim());
	}
	
}
